package ks.client.gui.admin.plugin.listeners;

import javax.swing.JList;

import ks.client.UserContext;
import ks.client.lobby.Lobby;
import ks.managers.plugin.Plugin;
import ks.managers.plugin.PluginManager;

/**
 * Updates the two plugin lists every time a plugin request is received.
 * 
 * @author Jennifer Hunt
 *
 */
public class UpdatePluginLists {
	Lobby lobby;
	UserContext  context;
	PluginManager pm;
	String names[];
	public UpdatePluginLists(Lobby lobby){
		this.lobby = lobby;
		this.context = lobby.getUserContext();
		this.pm = context.getPluginManager();
		makeList();
		makeTable();
	}
	
	/**
	 * Updates the table in the admin plugin window.
	 * 
	 */
	public void makeTable(){
		lobby.getPluginAdminGUI().getModel().removeAll();
		for (Plugin plugin : pm.getList()) {
			new AddPluginController(lobby).add(plugin);
		}
		
	}
	
	/**
	 * Updates the list in the plugin manager gui.
	 * 
	 */
	public void makeList(){
		names = new String[pm.getList().size()];
		int i = 0;
		for (Plugin plugin : pm.getList()) {
			names[i] = plugin.getName();
			i++;
		}
		
		
		JList a = lobby.getPMGUI().getList();
		 a.setModel(new javax.swing.AbstractListModel() {
	            /**
				 * 
				 */
				private static final long serialVersionUID = 1L;
				String[] strings = names;
	            public int getSize() { return strings.length; }
	            public Object getElementAt(int i) { return strings[i]; }
	        });
	}
}
